/**
 * Created by lional on 16/10/3.
 */

jGame.ModelObject = jGame.GameObject.extend(/** @lends jGame.ModelObject# */{

    _data: null,
    _listenersMap: null,

    ctor: function(obj){
        this._super();
        if(typeof obj === "object") {
            this._data = obj;
        }else {
            this._data = {};
        }
        this._listenersMap = {};
        //cc.log("jGame.ModelObject#ctor()");
    },
    
    /**
     * Adds a Custom event listener. It will use a fixed priority of 1.
     *
     * var event = new jGame.EventCustom("game_custom_event");
     * event.setUserData(selfPointer._item2Count.toString());
     * jGame.eventManager.dispatchEvent(event);
     *
     * @param {string} eventName
     * @param {function} callback
     * @return {cc.EventListener} the generated event. Needed in order to remove the event from the dispatcher
     */
    addEventListener: function (eventName, callback) {
        if(this.getEventListeners(eventName)) {
            this.removeEventListener(eventName);
        }
        this._listenersMap[eventName] = cc.EventListener.create({
            event: cc.EventListener.CUSTOM,
            eventName: eventName,
            callback: callback
        });
        cc.eventManager.addListener(this._listenersMap[eventName], 1);
    },

    addAsyncEventListener: function (eventName, callback) {
        if(this.getEventListeners(eventName)) {
            this.removeEventListener(eventName);
        }
        var asyncCallBack = function(e){
            if(app) {
                var scene = app.getRunningScene();
                if(scene) {
                    var sceneCallBack = function (send, event) {
                        callback(event);
                    }
                    scene.runAction(cc.sequence(
                        cc.delayTime(0),
                        cc.callFunc(sceneCallBack, scene, e)  // If you want to pass a optional value, like "Hi!", then you should pass 'this' too
                    ));
                }
            }
        }
        this._listenersMap[eventName] = cc.EventListener.create({
            event: cc.EventListener.CUSTOM,
            eventName: eventName,
            callback: asyncCallBack
        });
        cc.eventManager.addListener(this._listenersMap[eventName], 1);
    },

    getEventListeners: function (eventName) {
        return this._listenersMap[eventName];
    },

    removeEventListener: function (eventName) {
        cc.eventManager.removeListener(this._listenersMap[eventName]);
    },

    removeAllEventListeners: function () {
        for (var index in this._listenersMap) {
            cc.eventManager.removeListener(this._listenersMap[index]);
        }
    },

    setData: function (key, value) {
        if(typeof key === "string" || typeof key === "number") {
            this._data[key] = value;
        }else {
            cc.log("Error : key must be string or number!");
        }
    },

    deleteData: function (key) {
        if(typeof key === "string" || typeof key === "number") {
            delete this._data[key];
        }else {
            cc.log("Error : key must be string or number!");
        }
    },

    getData: function (key) {
        if(typeof key === "string" || typeof key === "number") {
            return this._data[key];
        }else {
            cc.log("Error : key must be string or number!");
            return undefined;
        }
    },

    saveData: function (save_key) {
        jGame.saveData("MODEL_OBJECT_"+save_key, JSON.stringify(this._data));
    },

    loadData: function (save_key, default_value) {
        var v = typeof default_value === "object"?JSON.stringify(default_value):default_value;
        if(typeof default_value === "undefined")v = "{}";
        this._data = JSON.parse(jGame.getData("MODEL_OBJECT_"+save_key, v));
        return this._data;
    },
});